package com.wkh.datastructure.ReverseLinkedList;


import java.io.BufferedReader;

//反转链表----方法二
public class demo2 {
    public static void main(String[] args) {
        ListNode node4 = new ListNode(null, 4);
        ListNode node3 = new ListNode(node4, 3);
        ListNode node2 = new ListNode(node3, 2);
        ListNode node1= new ListNode(node2, 1);
        ListNode reverse = reverse(node1);
    }

    private static ListNode reverse(ListNode node1) {
        List list1 = new List(node1);
        List list2 = new List(null);
        while (true){
            ListNode removed = list1.removeFirst();
            if (removed == null){
                break;
            }
            list2.addFirst(removed);
        }
        return list2.head;
    }

    static class List{
        ListNode head;

        public List(ListNode head) {
            this.head = head;
        }
        public void addFirst(ListNode first){
            first.next = head;
            head = first;
        }
        public ListNode removeFirst(){
            ListNode first = this.head;
            if (first != null){
                head = first.next;
            }
            return first;
        }
    }
}
